/*
 * Copyright 2007, 2008 Tiberiumlabs
 * 
 * This file is part of Tiberiumlabs Shards.
 * 
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.screenshot;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JSpinner;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class ScreenshotAction extends AbstractAction {
    
    private ImageComponent imageComponent;
    private JSpinner delaySpinner;
    private JCheckBox hideWindowCheckbox;
    private JFrame frame;
    private Robot robot;

    public ScreenshotAction(ImageComponent imageComponent, JSpinner delaySpinner, JCheckBox hideWindowCheckbox, JFrame frame, Robot robot) {
        this.imageComponent = imageComponent;
        this.delaySpinner = delaySpinner;
        this.hideWindowCheckbox = hideWindowCheckbox;
        this.frame = frame;
        this.robot = robot;
        putValue(NAME, "New Screenshot"); 
        putValue(SHORT_DESCRIPTION, "New Screenshot");
        putValue(LONG_DESCRIPTION, "New Screenshot");
    }

    public void actionPerformed(ActionEvent e) {
        
        if (robot == null) {
            try {
                robot = new Robot();
            } catch (Exception ex) {
                return;
            }
        }
        
        if (hideWindowCheckbox.isSelected()) {
            frame.dispose();
        }

        Integer time = (Integer) delaySpinner.getValue();
        if (time != null && time.intValue() > 0) {
            try {
                Thread.sleep(time.intValue() * 1000);
            } catch (Exception ex) {
            }
        }
        
        imageComponent.setImage(robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())));
        imageComponent.repaint();
        if (hideWindowCheckbox.isSelected()) {
            frame.setVisible(true);
        }
    }

}
